﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Shared
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.Albums;

    public class AlbumImageRetrievedEventArgs : EventArgs
    {
        private int currentPage;
        private int totalPages;

        public int CurrentPage { get { return currentPage; } }
        public int TotalPages { get { return totalPages; }  }
        private Sc_AlbumImage imageItem;
        public Sc_AlbumImage AlbumImageItem { get { return imageItem; } }
        private AlbumsWithCoverImage albumItem;
        public AlbumsWithCoverImage AlbumItem { get { return albumItem; } }


        public AlbumImageRetrievedEventArgs(Sc_AlbumImage item, AlbumsWithCoverImage album, int current, int total)
        {
            imageItem = item;
            albumItem = album;
            currentPage = current;
            totalPages = total;
        }
    }

    public partial class ViewAlbumImage : UserControl
    {
        static Sc_AlbumImage image;
        protected static AlbumsWithCoverImage album;
        static string cssClass = string.Empty;
        static List<Sc_AlbumImage> imageList = null;
        protected static int currentImageIndex;
        Guid albumId;


        public Guid AlbumId { get { return albumId; } set { albumId = value; } }
        //public int CurrentImageIndex
        //{
        //    get { return (ViewState["currentImageIndex"]==null)?0:(int)ViewState["currentImageIndex"]; }
        //    set { ViewState["currentImageIndex"] = value; }
        //}

        public int CurrentImageIndex
        {
            get { return currentImageIndex; }
            set 
            {
                if (!IsPostBack)
                {
                    currentImageIndex = value;
                }
            }
        }

        public List<Sc_AlbumImage> ImageList
        {
            get { return imageList; }
            set { if (imageList == null) imageList = value; }
        }

        public string CssClass { get { return cssClass; } set { cssClass = value; } }

        public event EventHandler<AlbumImageRetrievedEventArgs> AlbumImageRetrieved;

        //public Guid ImageId { get { return imageId; } set { imageId = value; } }
        //private string imageIdQueryStringField;
        //public string ImageIdQueryStringField { get { return imageIdQueryStringField; } set { imageIdQueryStringField = value; } }
        protected void Page_Init(object sender, EventArgs e)
        {
            ListView1.ItemCommand += new EventHandler<ListViewCommandEventArgs>(ListView1_ItemCommand);
        }

        void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "GoPrevious":
                    GoPrevious();
                    break;
                case "GoNext":
                    GoNext();
                    break;
                default: break;
            }
        }

        private void GoNext()
        {
            if (currentImageIndex == album.PhotoCount - 1)
            {
                currentImageIndex = 0;
            }
            else
            {
                currentImageIndex += 1;
            }
            BindList();
        }

        private void GoPrevious()
        {
            if (currentImageIndex == 0)
            {
                currentImageIndex = album.PhotoCount-1;
            }
            else
            {
                currentImageIndex -= 1;
            }
            
            BindList();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (cssClass != string.Empty && cssClass != null)
            {
                Panel1.CssClass = cssClass;
            }

            if (!IsPostBack)
            {
                album = AlbumOperations.GetAlbum(albumId);
                imageList = AlbumOperations.GetAlbumImages(albumId); 
            }

            BindList();
        }

        void BindList()
        {
            image = imageList[currentImageIndex];
            List<Sc_AlbumImage> currentImage = new List<Sc_AlbumImage>();
            currentImage.Add(image);
            ListView1.DataSource = currentImage;
            ListView1.DataBind();
            AlbumImageRetrieved(this, new AlbumImageRetrievedEventArgs(image, album, currentImageIndex, album.PhotoCount));
        }
    }
}